/**
 * Framework for batch testing user profile models
 * Final project by Sergey Nepomnyachiy and Julia Polchin
 * Supervisor: Tsvi Kuflik
 *
 */
package models.content;

import java.util.Vector;

/**
 * @author Sergey and Julia
 * 
 */
public class Formulae {

	/**
	 * @param a
	 * @param b
	 * @return
	 */
	static public double cosine(Vector<Double> a, Vector<Double> b) {
		assert (a.size() <= b.size());
		double dotab = dotProd(a, b); 
		double normab = norm(a) * norm(b);
		assert( normab != 0.0);
		return Math.acos(dotab / normab );
	}

	/**
	 * @param a
	 *            first vector
	 * @param b
	 *            second vector
	 * @return dot product of 2 vectors
	 */
	@SuppressWarnings("boxing")
	static public double dotProd(Vector<Double> a, Vector<Double> b) {
		double result = 0.0;
		for (int i = 0; i < a.size(); i++) {
			result += a.elementAt(i) * b.elementAt(i);
		}
		return result;
	}

	/**
	 * @param vec
	 *            Vector to normalize
	 * @return norm. of the given vector
	 */
	@SuppressWarnings("boxing")
	static public double norm(Vector<Double> vec) {
		Double squares = 0.0;
		for (Double i : vec)
			squares += (i * i);
		return Math.sqrt(squares);
	}
}